package dao.impl;

import dao.ChatDao;
import domian.Chat;
import domian.Comment;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChatDaoImpl implements ChatDao {
    @Override
    public Integer addChat(Chat chat) {
        String sql ="insert into  chat(send_id,receipt_id,content,send_time) values(?,?,?,?)";
        Object[] arr={chat.getSend_id(),chat.getReceipt_id(),chat.getContent(),chat.getSend_time()};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public List<Chat> findAllChat() {
        List<Chat> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建SQL语句
            String sql = "select * from chat ";
            //获取SQL语句对象
            ps = connection.prepareStatement(sql);
            Chat chat= null;
            //执行SQL语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer cId = resultSet.getInt("c_id");
                int id = resultSet.getInt("send_id");
                int id1 = resultSet.getInt("receipt_id");
                String content = resultSet.getString("content");
                String sendTime = resultSet.getString("send_time");
                chat=  new Chat(cId,id,id1,content,sendTime);
                list.add(chat);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            BaseDAO.close(connection, ps, resultSet);
        }
        return list;
    }
}
